async function updateLeaderboard(playerName, level, currentScores) {
    try {
        return await retryOperation(async () => {
            const trickleObjAPI = new TrickleObjectAPI();
            const historicalObjectType = 'colorGameScore';
            const currentDate = getCurrentUTCDateString();
            const dailyObjectType = `colorGameScore_${currentDate}`;

            // Check if we need to update daily score
            let updatedDaily = false;
            let dailyScores = currentScores.daily;
            const existingDailyScore = dailyScores.find(score => 
                score.objectData.playerName.toLowerCase() === playerName.toLowerCase()
            );

            // Check if we need to update historical score
            let updatedHistorical = false;
            let historicalScores = currentScores.historical;
            const existingHistoricalScore = historicalScores.find(score => 
                score.objectData.playerName.toLowerCase() === playerName.toLowerCase()
            );

            const timestamp = new Date().toISOString();
            const verificationToken = await generateScoreToken(level, playerName, timestamp);
            const scoreData = {
                playerName,
                score: level,
                timestamp,
                verificationToken
            };

            // Update daily score if it's better than today's score
            if (!existingDailyScore) {
                const newDailyScore = await trickleObjAPI.createObject(dailyObjectType, scoreData);
                dailyScores.push({
                    ...newDailyScore,
                    isVerified: true
                });
                updatedDaily = true;
            } else if (level > existingDailyScore.objectData.score) {
                const updatedDailyScore = await trickleObjAPI.updateObject(
                    dailyObjectType,
                    existingDailyScore.objectId,
                    scoreData
                );
                
                // Update local daily scores
                dailyScores = dailyScores.map(score => 
                    score.objectData.playerName.toLowerCase() === playerName.toLowerCase()
                        ? { ...updatedDailyScore, isVerified: true }
                        : score
                );
                updatedDaily = true;
            }

            // Update historical score if it's a new all-time high score
            if (!existingHistoricalScore) {
                const newHistoricalScore = await trickleObjAPI.createObject(historicalObjectType, scoreData);
                historicalScores.push({
                    ...newHistoricalScore,
                    isVerified: true
                });
                updatedHistorical = true;
            } else if (level > existingHistoricalScore.objectData.score) {
                const updatedHistoricalScore = await trickleObjAPI.updateObject(
                    historicalObjectType,
                    existingHistoricalScore.objectId,
                    scoreData
                );
                
                // Update local historical scores
                historicalScores = historicalScores.map(score => 
                    score.objectData.playerName.toLowerCase() === playerName.toLowerCase()
                        ? { ...updatedHistoricalScore, isVerified: true }
                        : score
                );
                updatedHistorical = true;
            }

            // Sort scores if needed
            if (updatedDaily) {
                dailyScores.sort((a, b) => {
                    if (b.objectData.score !== a.objectData.score) {
                        return b.objectData.score - a.objectData.score;
                    }
                    return new Date(a.objectData.timestamp) - new Date(b.objectData.timestamp);
                });
            }

            if (updatedHistorical) {
                historicalScores.sort((a, b) => {
                    if (b.objectData.score !== a.objectData.score) {
                        return b.objectData.score - a.objectData.score;
                    }
                    return new Date(a.objectData.timestamp) - new Date(b.objectData.timestamp);
                });
            }

            return {
                historical: historicalScores,
                daily: dailyScores
            };
        }); // retryOperation will now use default 5 retries
    } catch (error) {
        reportError(error);
        throw error; // Let the caller handle the error
    }
}

async function getLeaderboard(type = 'daily') {
    try {
        return await retryOperation(async () => {
            const trickleObjAPI = new TrickleObjectAPI();
            const currentDate = getCurrentUTCDateString();
            const objectType = type === 'historical' ? 'colorGameScore' : `colorGameScore_${currentDate}`;
            
            // Get all scores
            const { items } = await trickleObjAPI.listObjects(objectType, 1000000, true);
            
            // Add verification status to scores
            const verifiedScores = await addVerificationStatus(items);
            return verifiedScores;
        }); // retryOperation will now use default 5 retries
    } catch (error) {
        reportError(error);
        throw error; // Let the caller handle the error
    }
}

async function addVerificationStatus(scores) {
    try {
        const scoresWithVerification = await Promise.all(
            scores.map(async (score) => ({
                ...score,
                isVerified: await verifyScoreObject(score)
            }))
        );
        return scoresWithVerification;
    } catch (error) {
        reportError(error);
        return scores;
    }
}
